﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		DateTimeExtensions.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            DateTimeExtensions.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using Dexter.Extensions.Types;

namespace System
{
	///<summary>
	///	Add functions to the DateTime struct.
	///</summary>
	public static class DateTimeExtensions
	{
		/// <summary>
		/// 	Ages the specified birth date.
		/// </summary>
		/// <param name = "birthDate">The birth date.</param>
		/// <param name = "fromASpecificDate">From A specific date.</param>
		/// <returns></returns>
		public static int Age ( this DateTime birthDate , DateTime fromASpecificDate )
		{
			// get the difference in years
			int years = fromASpecificDate.Year - birthDate.Year;

			// subtract another year if we're before the
			// birth day in the current year
			if ( fromASpecificDate.Month < birthDate.Month ||
			     ( fromASpecificDate.Month == birthDate.Month &&
			       fromASpecificDate.Day < birthDate.Day ) )
				years--;

			return years;
		}

		/// <summary>
		/// 	Ages the specified birth date.
		/// </summary>
		/// <param name = "birthDate">The birth date.</param>
		/// <returns></returns>
		public static int Age ( this DateTime birthDate )
		{
			return birthDate.Age ( DateTime.Now );
		}
		
		/// <summary>
		/// 	Converts the value of the specified <see cref = "DateTime" /> object to its equivalent string representation.
		/// </summary>
		/// <param name = "dateTime">The date to convert.</param>
		/// <returns>A RFC 822 string representation of the value of the <paramref name = "dateTime" />.</returns>
		/// <exception cref = "ArgumentException">The specified <paramref name = "dateTime" /> object does not represent a <see cref = "DateTimeKind.Utc">Coordinated Universal Time (UTC)</see> value.</exception>
		public static string ToRfc822 ( this DateTime dateTime ) {
			if (dateTime.Kind != DateTimeKind.Utc)
				dateTime = dateTime.ToUniversalTime ( );

			return Rfc822DateTime.ToString ( dateTime );
		}

		/// <summary>
		/// 	Converts the value of the specified <see cref = "DateTime" /> object to its equivalent string representation.
		/// </summary>
		/// <param name = "dateTime">The date to convert.</param>
		/// <returns>A RFC 822 string representation of the value of the <paramref name = "dateTime" />.</returns>
		/// <exception cref = "ArgumentException">The specified <paramref name = "dateTime" /> object does not represent a <see cref = "DateTimeKind.Utc">Coordinated Universal Time (UTC)</see> value.</exception>
		public static string Rfc3339 ( this DateTime dateTime ) {
			if (dateTime.Kind != DateTimeKind.Utc)
				dateTime = dateTime.ToUniversalTime ( );

			return Rfc3339DateTime.ToString ( dateTime );
		}
	}
}
